package org.medusa.module;
/*
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
*/
//1.检测starup中的starup
//2.在1的基础上推断出父子关系

//3.如果让用户自己去填写依赖项，那么用户可能会犯错，所以必须智能判断依赖

//综上所述：以来检测只用来防御互相调用starup

public final class ModuleController {
	/*
	private static Map<Integer, Module> id_mod_link = new HashMap<Integer, Module>();
	private static Map<Class<?>, Module> class_mod_link = new HashMap<Class<?>, Module>();
	private static AtomicInteger index = new AtomicInteger(1);
	
	//public static void Add() {}

	public static void Restart() {
		Iterator<Module> it = ModuleController.id_mod_link.values().iterator();
		while (it.hasNext()) {
			Module mod = it.next();
			if (mod != null)
				mod.set_started(mod.event_restart() == null);
		}
	}

	public static void Shutdown() {
		Iterator<Module> it = ModuleController.id_mod_link.values().iterator();
		while (it.hasNext()) {
			Module mod = it.next();
			if (mod != null) {
				mod.event_shutdown();
				mod.set_started(false);
			}
		}
		ModuleController.id_mod_link.clear();
		ModuleController.class_mod_link.clear();
	}

	private static int todo_event = 0;

	protected static void require_start(Module mod) {
		if (mod != null && ModuleController.todo_event == 0) {
			ModuleController.todo_event = mod.ModID();
		} else
			System.err.println();
	}

	public static Module Get(int mod_id) {
		return ModuleController.id_mod_link.get(mod_id);
	}

	@SuppressWarnings("unchecked")
	public static <T extends Module> T Query(Class<T> classType) {
		// Module mod = null;
		// Constructor<?> cons = null;
		if (!ModuleController.class_mod_link.containsKey(classType)) {
			try {
				Constructor<?> cons = classType.getConstructor();
				Module mod = (Module) cons.newInstance();

				if (mod != null && Module.class.isInstance(mod)) {
					System.err.println("module " + mod.getClass().getName()
							+ " created.");
					Error err = mod.event_startup();
					if (err == null) {
						mod.set_started(true);
						System.err.println("module " + mod.getClass().getName()
								+ " startup success");
						ModuleController.id_mod_link.put(mod.ModID(), mod);
						ModuleController.class_mod_link
								.put(mod.getClass(), mod);
					} else
						System.err.println("module " + mod.getClass().getName()
								+ " startup failed");

				}
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}

		return (T) ModuleController.class_mod_link.get(classType);
	}

	protected static void register(Module mod) {
		if (mod != null) {
			if ( ModuleController.class_mod_link.containsKey(mod
							.getClass().getName()))
				mod.setModID(ModuleController.index.addAndGet(1));
			// else
			// System.err.println("found invalid use of module : "
			// + mod.getClass().getName());
		}

	}
*/
}
